<?php
declare(strict_types=1);

namespace SixShop\Lakala\Service;

use SixShop\Lakala\Config;
use SixShop\Lakala\Enum\UploadFileTypeEnum;
use SixShop\Lakala\OpenAPISDK\V2\Api\V2LakalaApi;
use SixShop\Lakala\OpenAPISDK\V2\Model\V2ModelRequest;
use function SixShop\Core\throw_logic_exception;

class MMSService
{
    private V2LakalaApi $v2LakalaApi;

    public function __construct(private Config $config)
    {
        $this->v2LakalaApi = new V2LakalaApi($config->getV2Config());
    }

    /**
     * 卡BIN查询
     *
     * @param string $orderNo 订单编号（便于后续跟踪排查问题及核对报文） 14位年月日时（24小时制）分秒+8位的随机数（不重复）如：2021020112000012345678
     * @param string $cardNo 银行卡号
     * @param string $orgCode 机构代码
     * @param string $version 接口版本号 默认1.0
     * @link https://o.lakala.com/#/home/document/detail?id=179
     */
    public function cardBin(string $orderNo, string $cardNo,string $orgCode = '1',  string $version = '1.0'):object
    {
        $request = new V2ModelRequest();
        $request->setReqData([
            'version' => $version,
            'orderNo' => $orderNo,
            'orgCode' => $orgCode,
            'cardNo' => $cardNo,
        ]);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/cardBin', $request);
        if ($response->getRetCode() == '000000') {
            $result = $response->getRespData();
            if ($result->bankCode && $result->clearingBankCode) {
                return $result;
            } else {
                throw_logic_exception('未获取到开户行信息');
            }
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }

    /**
     * 附件上传
     *
     * @param string $orderNo
     * @param UploadFileTypeEnum $attType
     * @param string $attExtName
     * @param string $fileContent
     * @param string $orgCode
     * @param string $version
     *
     * @link https://o.lakala.com/#/home/document/detail?id=90
     */
    public function uploadFile(string $orderNo, UploadFileTypeEnum $attType, string $attExtName, string $fileContent, string $orgCode = '1',  string $version = '1.0')
    {
        $request = new V2ModelRequest();
        $attContext = base64_encode($fileContent);
        $request->setReqData([
            'attContext' => $attContext,
            'attType' => $attType->value,
            'attExtName' => $attExtName,
            'orderNo' => $orderNo,
            'orgCode' => $orgCode,
            'version' => $version,
        ]);
        $response = $this->v2LakalaApi->tradeApi('/api/v2/mms/openApi/uploadFile', $request);

        if ($response->getRetCode() == '000000') {
            return $response->getRespData();
        } else {
            throw_logic_exception(
                msg:$response->getRetMsg(),
                code: (int)$response->getRetCode(),
                data: $response->getRespData(),
            );
        }
    }
}